<script setup lang="ts">
import {getPreOrderAPI, getPatientDetailAPI, createOrderAPI, getOrderPayUrlAPI} from '@/services/consult'
import { ref } from 'vue';
import type { Patient } from '@/types/patient';
import type { OrderPreData } from '@/types/consult';
import { useConsultStore } from '@/stores/consult';
import { showToast } from 'vant';

// 获取仓库
const store = useConsultStore()

// 创建变量存放数据
const patient = ref<Patient | null>(null)
const preOrder = ref<OrderPreData | null>(null)

// 定义获取数据的函数并执行
const getPreOrder = async () => {
  const res = await getPreOrderAPI({
    type: store.consultForm.type,
    illnessType: store.consultForm.illnessType,
  })
  preOrder.value = res.data
} 

const getPatient = async () => {
  if(!store.consultForm.patientId) return

  const res = await getPatientDetailAPI(store.consultForm.patientId)
  patient.value = res.data
} 

getPreOrder()
getPatient()

// 弹出支付方式弹窗
const isShowPay = ref(false)
// 储存选择的支付方式 0 是微信, 1 是支付宝
const paymentMethod = ref()
// 立即支付
const payNow = async () => {
  // 不能没选支付方式, 也不能选微信支付
  if(paymentMethod.value === undefined) return showToast('请选择支付方式')
  if(paymentMethod.value === 0) return showToast('目前只支持支付宝支付')
  // 支付宝支付
  // 1. 创建订单
  const res = await createOrderAPI(store.consultForm)
  const orderId = res.data.id
  // 2. 利用订单id, 创建支付单, 获取支付地址
  const res2 = await getOrderPayUrlAPI({
    paymentMethod: paymentMethod.value,
    orderId,
    // 这个参数是付款完毕, 支付宝调回来的地址
    // payCallback: 'http://localhost:5173/user'
    payCallback: 'http://localhost:5173/room'
  })
  const payUrl = res2.data.payUrl
  // 3. 跳转支付页面
  // 准备给钱了, 之前的哪些填写的表单就应该清空
  // 下次回来问诊又是全新的表单了
  store.clearConsultForm()
  // 然后跳转支付
  window.location = payUrl
}
</script>

<template>
  <div class="consult-pay-page">
    <!-- 导航栏 -->
    <cp-nav-bar title="支付" />

    <!-- 费用信息 -->
    <div class="pay-info">
      <p class="tit">图文问诊 {{preOrder?.payment}} 元</p>
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <p class="desc">
        <span>极速问诊</span>
        <span>自动分配医生</span>
      </p>
    </div>

    <!-- 折扣 -->
    <van-cell-group>
      <van-cell title="优惠券" :value="preOrder?.couponDeduction" />
      <van-cell title="积分抵扣" :value="preOrder?.pointDeduction" />
      <van-cell title="实付款" :value="preOrder?.actualPayment" class="pay-price" />
    </van-cell-group>
    <div class="pay-space"></div>

    <!-- 患者信息 -->
    <van-cell-group>
      <van-cell title="患者信息" :value="`${patient?.name} | ${patient?.genderValue} | ${patient?.age}岁`"></van-cell>
      <van-cell title="病情描述" :label="store.consultForm.illnessDesc"></van-cell>
    </van-cell-group>
    <div class="pay-schema">
      <van-checkbox>我已同意 <span class="text">支付协议</span></van-checkbox>
    </div>

    <!-- 底部支付按钮 -->
    <!-- 这里默认以分为单位来储存支付数据 -->
    <!-- 原因是 js 小数精度溢出(丢失) -->
    <van-submit-bar
      @submit="isShowPay = true"
      button-type="primary"
      :price="preOrder? preOrder?.actualPayment * 100 : 0"
      button-text="立即支付"
      text-align="left"
    />
    
    <!-- 支付选项卡片 -->
    <van-action-sheet v-model:show="isShowPay" title="选择支付方式">
      <div class="pay-type">
        <p class="amount">￥{{preOrder?.actualPayment}}元</p>
        <van-cell-group>
          <van-cell title="微信支付" @click="paymentMethod = 0">
            <template #icon><cp-icon name="consult-wechat" /></template>
            <template #extra><van-checkbox :model-value="paymentMethod === 0" /></template>
          </van-cell>
          <van-cell title="支付宝支付" @click="paymentMethod = 1">
            <template #icon><cp-icon name="consult-alipay" /></template>
            <template #extra><van-checkbox :model-value="paymentMethod === 1" /></template>
          </van-cell>
        </van-cell-group>
        <div class="btn">
          <van-button type="primary" round block @click="payNow">立即支付</van-button>
        </div>
      </div>
    </van-action-sheet>
  </div>
</template>

<style lang="scss" scoped>
.consult-pay-page {
  padding: 0 0 50px;
}

.pay-info {
  display: flex;
  padding: 15px;
  flex-wrap: wrap;
  align-items: center;
  .tit {
    width: 100%;
    font-size: 16px;
    margin-bottom: 10px;
  }
  .img {
    margin-right: 10px;
    width: 38px;
    height: 38px;
    border-radius: 4px;
    overflow: hidden;
  }
  .desc {
    flex: 1;
    > span {
      display: block;
      color: var(--cp-tag);
      &:first-child {
        font-size: 16px;
        color: var(--cp-text2);
      }
    }
  }
}

.pay-price {
  ::v-deep() {
    .vam-cell__title {
      font-size: 16px;
    }
    .van-cell__value {
      font-size: 16px;
      color: var(--cp-price);
    }
  }
}
.pay-space {
  height: 12px;
  background-color: var(--cp-bg);
}

.pay-schema {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  .text {
    color: var(--cp-primary);
  }
}

::v-deep() {
  .van-submit-bar__button {
    font-weight: normal;
    width: 160px;
  }
}
  
.pay-type {
  .amount {
    padding: 20px;
    text-align: center;
    font-size: 16px;
    font-weight: bold;
  }
  .btn {
    padding: 15px;
  }
  .van-cell {
    align-items: center;
    .cp-icon {
      margin-right: 10px;
      font-size: 18px;
    }
    .van-checkbox :deep(.van-checkbox__icon) {
      font-size: 16px;
    }
  }
}
</style>